package _022_2_12

type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func levelOrder(root *TreeNode) []int {
	if root == nil {
		return nil
	}

	result := make([]int, 0)

	nodes := make([]*TreeNode, 0)

	nodes = append(nodes, root)

	for len(nodes) > 0 {
		l := len(nodes)
		for i := 0; i < l; i++ {
			if nodes[i] == nil {
				continue
			}
			result = append(result, nodes[i].Val)
			nodes = append(nodes, nodes[i].Left)
			nodes = append(nodes, nodes[i].Right)
		}
		nodes = nodes[l:]
	}

	return result
}
